package com.dixon.worddayday.controller;

import com.dixon.worddayday.bo.CustomerSession;
import com.dixon.worddayday.config.security.JwtTokenProvider;
import com.dixon.worddayday.dto.CustomerInfo;
import com.dixon.worddayday.dto.ProgressStatistical;
import com.dixon.worddayday.dto.UserProfiles;
import com.dixon.worddayday.request.*;
import com.dixon.worddayday.response.BaseRes;
import com.dixon.worddayday.response.ResponseCode;
import com.dixon.worddayday.service.CustomerService;
import com.dixon.worddayday.service.ProgressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@RestController
public class CustController extends BaseController{
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProgressService progressService;

    @PostMapping("/cust/login")
    public BaseRes<String> login(@RequestBody @Valid LoginReq loginReq) {
        String username = loginReq.getUsername();
        String password = loginReq.getPassword();
        try {
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
            String token = jwtTokenProvider.generateToken(username);
            return new BaseRes<>(token);
        } catch (AuthenticationException e) {
            return BaseRes.<String>builder().code(ResponseCode.BUSINESS_EXCEPTION.getCode()).desc("用户名或密码错误").build();
        }
    }

    @PostMapping("/cust/registry")
    public BaseRes register(@RequestBody @Valid RegisterReq registerReq) {
        return customerService.register(registerReq);
    }

    @GetMapping("/cust/info")
    public BaseRes<String> info(@ModelAttribute @Valid BaseReq req) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();
        return new BaseRes<>(customerSession.getUsername());
    }

    @GetMapping("/cust/profiles")
    public BaseRes<UserProfiles> profiles(@ModelAttribute @Valid BaseReq req) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        ProgressStatistical statistical = progressService.statistical(customerSession.getCustomer());

        UserProfiles userProfiles = new UserProfiles();

        CustomerInfo customerInfo = new CustomerInfo();
        BeanUtils.copyProperties(customerSession.getCustomer(), customerInfo);

        userProfiles.setCustInfo(customerInfo);
        userProfiles.setStatistical(statistical);

        return new BaseRes<>(userProfiles);
    }
}
